package leetcode;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Set;

/**
 * banana --> ban
 *
 * create by duanx 2021/1/9
 *@description： 1-9开发考试用类
 *@classname: Exam
 *@author: duanx
 *@date: 2021/1/9 10:47
 **/
public class Exam {

    public static String intercept(String s){
        //拆成字符
        char[] chars = s.toCharArray();
        LinkedList<Character> list = new LinkedList<Character>();
        HashMap<Character, Object> map = new HashMap<Character, Object>(32);
        Object o = new Object();
        //如果map里没有该字符，则字符放入map
        for(int i = 0; i < chars.length; i++) {
            if (!map.containsKey(chars[i])) {
                map.put(chars[i], o);
                list.add(chars[i]);
            }
        }
        StringBuilder str = new StringBuilder();
        for(int i = 0; i < list.size(); i++){
            str.append(list.get(i));
        }
        return str.toString();
    }

    public static HashMap<Character, Integer> countCharacter(String str){
        HashMap<Character, Integer> map = new HashMap<>();
        //转换为字符数组
        char[] chars = str.toCharArray();
        //遍历数组
        for (Character ch:chars
        ) {
            //如果集合中key包含此键，则取得值再+1，然后重新给键值
            if (map.containsKey(ch)){
                Integer num = map.get(ch);
                num++;
                map.put(ch, num);
            }
            else {
                //没有包含，则给value赋值1
                map.put(ch,1);
            }
        }
        return map;
    }
    public static void main(String[] args) {
        String s = "152726199409252420";
        System.out.println(intercept(s));
        String str = s;
        HashMap<Character,Integer> map = countCharacter(str);
        Set<Character> characters = map.keySet();
        for ( Character key:characters
        ) {
            Integer integer = map.get(key);
            System.out.println(key+"="+integer);
        }
        str = "422126199504224017";
        map = countCharacter(str);
        characters = map.keySet();
        for ( Character key:characters
        ) {
            Integer integer = map.get(key);
            System.out.println(key+"="+integer);
        }
    }

}
